#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 10
/* external variables */
char contests[STACK_SIZE];
int top = 0;
bool overflow = false;
void static_overflow(void) {
    overflow = true;
}
void stack_underflow(void) {
    printf("Stack Underflow.\n");
    exit(EXIT_FAILURE);
}
void make_empty(void) {
    top = 0;
}
bool is_empty(void) {
    return top == 0;
}
bool is_full(void) {
    return top == STACK_SIZE;
}
void push(char i) {
    if (is_full())
        static_overflow();
    else
        contests[top++] = i;
}
char pop(void) {
    if (is_empty())
        stack_underflow();
    else
        return contests[--top];
}

int main(void)
{
    make_empty();
    printf("Enter parenteses and/or braces: ");
    char ch;
    bool is_nested_properly = true;
    while ((ch = getchar()) != '\n' && is_nested_properly) {
        switch (ch)
        {
        case '(': case '[': case '{': case '<':
            push(ch);
            if (overflow)
                is_nested_properly =  false;
            break;
        case ')':
            if (pop() != '(')
                is_nested_properly = false;
            break;
        case ']':
            if (pop() != '[')
                is_nested_properly = false;
            break;
        case '}':
            if (pop() != '{')
                is_nested_properly = false;
            break;
        case '>':
            if (pop() != '<')
                is_nested_properly = false;
            break;
        }
    }
    if (is_nested_properly)
        if (is_empty())
            is_nested_properly = true;
        else
            is_nested_properly = false;
    printf("Parentes/braces are%snested properly.\n", is_nested_properly ? " " : " not ");
    return 0;
}